package com.jd.onlinequery.common.web;


import com.jd.onlinequery.common.utils.StringUtil;

import static com.jd.onlinequery.common.utils.ApplicationResourcesUtil.getMessageCfg;

public final class WebserviceInvocationResult {

    private final static String EMPTY = "";

    private boolean isSucceeded = false;

    private String errorCode = EMPTY;

    private String errorMessage = EMPTY;

    private Object result = EMPTY;

    public WebserviceInvocationResult(boolean isSucceeded, String errorCode, String errorMessage, Object result) {
        this.isSucceeded = isSucceeded;
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        this.result = result;
    }

    public WebserviceInvocationResult(boolean isSucceeded, Object result) {
        this.isSucceeded = isSucceeded;
        this.result = result;
    }

    @Override
    public String toString() {
        return "WebserviceInvocationResult [isSucceeded=" + isSucceeded + ", errorCode=" + errorCode + ", errorMessage="
                + errorMessage + ", result=" + result + "]";
    }

    public String toJson() {
        return "{\"isSucceeded\":" + isSucceeded + ",\"errorCode\":" + errorCode + ",\"errorMessage\":" + errorMessage
                + ",\"result\":" + result + "}";
    }

    public boolean isSucceeded() {
        return isSucceeded;
    }

    public void setSucceeded(boolean isSucceeded) {
        this.isSucceeded = isSucceeded;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }
    
    
    public String getErrorMessage() {
    	if(StringUtil.isBlank(this.errorMessage)){
    		return autoErrorMessage();
    	 }else
    	 {
    		 return this.errorMessage;
    	 }
    }
    
    
    public String autoErrorMessage() {
        if(StringUtil.isNotBlank(this.getErrorCode())) {
            errorMessage = this.getErrmFromCfg(this.getErrorCode(), null);
        }
        return errorMessage;
    }
    
    public String autoErrorMessage(String[] params) {
        if(StringUtil.isNotBlank(this.getErrorCode())) {
            errorMessage = this.getErrmFromCfg(this.getErrorCode(), params);
        }
        return errorMessage;
    }

    /**
     * 从配置文件中获取错误信息
     *
     * @param errcode
     * @param params
     * @return
     */
    private  String getErrmFromCfg(String errcode, String[] params) {
        return getMessageCfg(errcode, params);
    }

}
